package com.pdstudio.carrecom.ui.activity.more;

import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.http.RequestParams;
import com.pdstudio.carrecom.R;
import com.pdstudio.carrecom.api.HttpExecuteJson;
import com.pdstudio.carrecom.api.ServiceHelper;
import com.pdstudio.carrecom.api.HttpExecuteJson.httpReturnJson;
import com.pdstudio.carrecom.app.UIHelper;
import com.pdstudio.carrecom.bean.UpdateVersion;
import com.pdstudio.carrecom.tools.StringUtils;
import com.pdstudio.carrecom.tools.UpdateManager;
import com.pdstudio.carrecom.ui.activity.ActivityMain;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

/**
 * 更多主页面
 * 类: ActivityMoreMain <br>
 * 描述: TODO <br>
 * 作者: xiangzhaoyu  <br>
 * 时间: 2015-5-18 上午11:20:49
 */
public class ActivityMoreMain extends Activity {
	
	private UpdateManager mUpdateManager;
	private LinearLayout mMoreUserProtocal,mMoreSuggestion,mMoreUpdate;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_more_main);

		InitialView();
	}


	private void InitialView(){
		
		mMoreUserProtocal=(LinearLayout)findViewById(R.id.more_userprotocal);
		mMoreUserProtocal.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(ActivityMoreMain.this,ActivityUserProtocal.class);
				startActivity(intent);
			}});

		mMoreSuggestion=(LinearLayout)findViewById(R.id.more_suggestion);
		mMoreSuggestion.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(ActivityMoreMain.this,ActivitySuggestion.class);
				startActivity(intent);
			}});
		
		mMoreUpdate=(LinearLayout)findViewById(R.id.more_updateversion);
		mMoreUpdate.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try {
					String version=getApplicationContext().getPackageManager().getPackageInfo(
							"com.pdstudio.carrecom", 0).versionName;
					HttpExecuteJson http = new HttpExecuteJson(
							ActivityMoreMain.this, mUpdateVersionListener);
					Map<String,Object> rps=new HashMap<String,Object>();
					rps.put("os", "android");
					rps.put("version", version);
					http.get(ServiceHelper.NewVersion, rps);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}});
		
	}
	
	private void DoJson(String json){
		UpdateVersion edv = null;
		try {
			edv = new Gson().fromJson(json,
					new TypeToken<UpdateVersion>() {
					}.getType());
			if ((edv != null)
					&& (edv.msg.equals(ServiceHelper.msg_code_sucess))) {
				boolean u=edv.status;
				if(u&&(!StringUtils.isEmpty(edv.apkurl))){
					mUpdateManager = new UpdateManager(
							ActivityMoreMain.this);
					mUpdateManager.checkUpdateInfo1(edv.apkurl);
				}else{
					UIHelper.ToastMessage(ActivityMoreMain.this, "当前版本已经是最新版本");
				}
			}else{
				UIHelper.ToastMessage(ActivityMoreMain.this, "当前版本已经是最新版本");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private httpReturnJson mUpdateVersionListener = new httpReturnJson() {

		@Override
		public void onSuccess(String result) {
			// TODO Auto-generated method stub
			DoJson(result);

		}

		@Override
		public void onFailure(int error, String msg) {
			// TODO Auto-generated method stub
			UIHelper.ToastMessage(ActivityMoreMain.this, "当前版本已经是最新版本");
		}

		@Override
		public void onCancel() {
			// TODO Auto-generated method stub
			UIHelper.ToastMessage(ActivityMoreMain.this, "当前版本已经是最新版本");
		}
	};

	
	
}
